package com.hetang.maijiayun.adaptertools.adapter;

import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;
import com.suning.api.DefaultSuningClient;
import com.suning.api.entity.transaction.OrdQueryRequest;
import com.suning.api.entity.transaction.OrdQueryResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.http.impl.cookie.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wanda on 11/4/15.
 */
public class SuningAdapter extends AdapterAbstract {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(SuningAdapter.class);

    @Override
    public Integer request(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        return null;
    }

    @Override
    public List<String> requestOrders(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        if (MapUtils.isEmpty(authorizesMap) || !authorizesMap.containsKey("url") || !authorizesMap.containsKey("appKey") || !authorizesMap.containsKey("secret") || !authorizesMap.containsKey("sessionKey")) {
            logger.error("该苏宁平台店铺没有授权信息或授权信息错误！！");
            return null;
        }
        DefaultSuningClient client = new DefaultSuningClient(authorizesMap.get("url"), authorizesMap.get("appKey"), authorizesMap.get("secret"));
        OrdQueryRequest req = new OrdQueryRequest();
        List<String> result = new ArrayList<String>();
        req.setStartTime(DateUtils.formatDate(request.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setEndTime(DateUtils.formatDate(request.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setPageNo(1);
        req.setPageSize(50);
        for (int i=0; i<3; i++) {
            if (0 == i) {
                req.setOrderLineStatus("10");
            } else if (1 == i) {
                req.setOrderLineStatus("20");
            } else {
                req.setOrderLineStatus("30");
            }
            logger.info("suning get order interface, request time is : {}, request param is : {}", System.currentTimeMillis(), JsonTools.createJsonString(req));
            OrdQueryResponse rsp = client.excute(req);
            logger.info("suning get order interface, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
            if (rsp == null || !"".equals(rsp.getCode())) {
                return null;
            }
            Integer page = 0;
            page = rsp.getSnhead().getPageTotal();
            while (page > 0) {
                req.setPageNo(page);
                rsp = client.excute(req);
                if (!"".equals(rsp.getCode())) {
                    logger.error("suning get order interface error, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
                    continue;
                }
                if (CollectionUtils.isEmpty(rsp.getSnbody().getOrders())) {
                    continue;
                }
                for (OrdQueryResponse.OrderQuery orderInfo : rsp.getSnbody().getOrders()) {
                    result.add(orderInfo.getOrderCode() + "");
                }
                page -- ;
            }
        }
        return result;
    }
}
